package com.yoga.app.web;

import com.yoga.app.annotation.LoginUser;
import com.yoga.app.annotation.UserTokenManage;
import com.yoga.common.appexception.AppException;
import com.yoga.common.appexception.YogaResult;
import com.yoga.common.bo.YogaUserBO;
import com.yoga.manager.app.AppYogaUserManager;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by hdx on 2020/10/14.
 */
@RestController
@RequestMapping("${basePath}")
public class AppYogaUserController {

    @Resource
    private AppYogaUserManager yogaUserManager;


    @Resource(name="stringRedisTemplate")
    private ValueOperations<String,String> vops;

    @PostMapping("/register")
    public YogaResult<YogaUserBO> adminRegister(@RequestBody YogaUserBO yogaUserBO) throws AppException {
        YogaUserBO result = yogaUserManager.register(yogaUserBO);

        return new YogaResult<>(result);
    }

    @PostMapping("/login")
    public YogaResult<YogaUserBO> login(@RequestBody YogaUserBO yogaUserBO) throws AppException {
        YogaUserBO result = yogaUserManager.login(yogaUserBO);
        UserTokenManage.getInstance(vops).setLoginUser(result);

        return new YogaResult<>(result);
    }

    @PostMapping("/update/passwd")
    public YogaResult<Boolean> updatePasswd(@RequestBody YogaUserBO yogaUserBO) throws AppException {
        Boolean result = yogaUserManager.updatePasswd(yogaUserBO);
        return new YogaResult<>(result);
    }

    @PostMapping("/exit")
    public YogaResult<Boolean> exit(@LoginUser YogaUserBO yogaUserBO) throws AppException{
        UserTokenManage.getInstance(vops).delete(yogaUserBO.getYogaUserId());
        return new YogaResult<>(true);
    }

    @GetMapping("/getById")
    public YogaResult<YogaUserBO> findById(Long yogaUserId) throws AppException {
        YogaUserBO result = yogaUserManager.getUser(yogaUserId);

        return new YogaResult<>(result);
    }

}
